<!DOCTYPE html>
<html>
	<head>
        <meta charset="UTF-8"> 
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
        <meta name="apple-mobile-web-app-capable" content="yes" />

        <!-- Base Css Files -->
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/jqueryui/ui-lightness/jquery-ui-1.10.4.custom.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap/css/bootstrap.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/font-awesome/css/font-awesome.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/fontello/css/fontello.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/animate-css/animate.min.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/css/component.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/magnific-popup/magnific-popup.css" rel="stylesheet" /> 
        <link href="__PUBLIC__/statics/coco-chat/assets/css/style.css" rel="stylesheet" type="text/css" />
        <link href="__PUBLIC__/statics/coco-chat/assets/css/style-responsive.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/pace/pace.css" rel="stylesheet" />
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/styles/metro/notify-metro.css" rel="stylesheet" type="text/css" />
        
        <!-- Extra CSS Files -->
        <link href="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap-validator/css/bootstrapValidator.min.css" rel="stylesheet"/>
        
        <!-- Custom Css Files -->
        <link href="__HOME_CSS__/style.css"  rel="stylesheet" />
    </head>
    
    <body>
    <!-- Begin page -->
	<div>
		<div class="sub-header-title">
			<h2>写邮件</h2>
		</div>
		<form role="form" id="add-form" class="form-horizontal" action="{:U('Mail/add')}">
		  	<div class="form-group">
				<label for="receiver_no" class="col-sm-2 control-label">接收会员编号</label>
				<div class="col-sm-5">
					<if condition="!empty($mail)">
						<input type="hidden" name="parent_id" value="{$mail['id']}"/>
					    <input type="text" class="form-control" id="receiver_no" name="receiver_no" readonly value="{$mail['receiver_no']}"/>
					<else/>
					    <input type="text" class="form-control" id="receiver_no" name="receiver_no"/>
					</if>
				</div>
				<div class="col-sm-5">
					<p class="help-block">会员编号必须真实存在（不能为当前编号或不在可视范围内,为空表示发送给系统）</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2 control-label">真实姓名</label>
				<div class="col-sm-5">
				  	<input type="text" class="form-control" readonly name="realname" value="系统管理员">
				</div>
				<div class="col-sm-5">
					<p class="help-block">会员编号的真实姓名，不能更改</p>
				</div>
			</div>
			<div class="form-group">
				<label for="title" class="col-sm-2 control-label"><span class="red_star">*</span>邮件标题</label>
				<div class="col-sm-5">
					<if condition="!empty($mail)">
					   <input type="text" class="form-control" id="title" name="title" readonly value="{$mail['title']}"/>
					<else/>
					   <input type="text" class="form-control" id="title" name="title"/>
					</if>
				</div>
				<div class="col-sm-5">
					<p class="help-block">必须在2~50个字符之间</p>
				</div>
			</div>
			<div class="form-group">
				<label for="content" class="col-sm-2 control-label"><span class="red_star">*</span>邮件内容</label>
				<div class="col-sm-5">
				  	<textarea name="content" type="text/plain" id="content" style="height: 300px;"></textarea>
				</div>
				<div class="col-sm-5">
					<p class="help-block">不允许为空</p>
				</div>
			</div>
			<div class="form-group">
				<label class="col-sm-2"></label>
				<div align="center" class="col-sm-5">
					<button type="submit" class="btn btn-success">发送</button>
				</div>
				<div class="col-sm-5"></div>
			</div>
		</form>
	</div>
	<!-- End of page -->	

	<!-- Base Js Files -->
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery/jquery-1.11.1.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap/js/bootstrap.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jqueryui/jquery-ui-1.10.4.custom.min.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-ui-touch/jquery.ui.touch-punch.min.js"></script>
 	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-detectmobile/detect.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/fastclick/fastclick.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-slimscroll/jquery.slimscroll.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/js/classie.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/nifty-modal/js/modalEffects.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/notify.min.js"></script>
    <script src="__PUBLIC__/statics/coco-chat/assets/libs/jquery-notifyjs/styles/metro/notify-metro.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/pace/pace.min.js"></script>
	
	<!-- Extra Js Files -->
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/statics/coco-chat/assets/libs/ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/statics/coco-chat/assets/libs/ueditor/ueditor.all.js"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="__PUBLIC__/statics/coco-chat/assets/libs/ueditor/lang/zh-cn/zh-cn.js"></script>
	<script src="__PUBLIC__/statics/coco-chat/assets/libs/bootstrap-validator/js/bootstrapValidator.min.js"></script>
	
	<!-- Custom Js Files -->
	<script src="__HOME_JS__/common.js"></script>

	<script>
		var ue = UE.getEditor('content',{
			 toolbars: [
	          [
	            'undo', //撤销
	            'redo', //重做
	            'bold', //加粗
	            'italic', //斜体
	            'underline', //下划线
	            'forecolor', //字体颜色
	            'strikethrough', //删除线
	            'subscript', //下标
	            'fontborder', //字符边框
	            'superscript', //上标
	            'formatmatch', //格式刷
	            'customstyle', //自定义标题
	            'paragraph', //段落格式
	            'fontfamily', //字体
	            'fontsize', //字号
	            'pasteplain', //纯文本粘贴模式
	            'selectall', //全选
	            'removeformat', //清除格式
	            'simpleupload', //单图上传
	            'justifyleft', //居左对齐
	            'justifyright', //居右对齐
	            'justifycenter', //居中对齐
	            'justifyjustify', //两端对齐
	            'backcolor', //背景色
	            'rowspacingtop', //段前距
	            'rowspacingbottom', //段后距
	            'lineheight', //行间距
	            'edittip ', //编辑提示
	          ]
	        ],
	        autoHeightEnabled: true,
	        autoFloatEnabled: true,
	        elementPathEnabled: false, //删除元素路径
		});
        
        //文本编辑器添加验证
		ue.addListener( 'contentChange', function( content ) {
            if (ue.getContentLength(true)>0) {
              $('#add-form').data('bootstrapValidator').updateElementStatus($('#add-form').find("textarea[name='content']"), "VALID", null );
            } else {
              $('#add-form').data('bootstrapValidator').updateElementStatus($('#add-form').find("textarea[name='content']"), "INVALID", null );
            }
        });

		$('#add-form').bootstrapValidator({
	        message: '',
	        trigger: 'blur',
	        excluded: [':disabled'],
	        submitHandler: function(validator, form, submitButton) {
		      	//询问框
				window.parent.layer.confirm('确认发送站内信吗？', {
					btn: ['确认','取消'], //按钮
				    cancel: function(index){ 
				    	window.parent.layer.close(index);
						$('#add-form').bootstrapValidator('disableSubmitButtons', false);
					}
				}, function(index){
					
					// 用ajax提交表单
			      	$.post(form.attr('action'), form.serialize(), function(response) {
			      		//弹出提示
						window.parent.layer.msg(response.message);
			      		
			      		//根据请求相应状态提示通知
			       		if(response.status){
			       			notify("success",response.message);
			       			window.location.href = "{:U('Mail/outbox')}";
						}else{
							notify("error",response.message);
						}
			        }, 'json');
			      	window.parent.layer.close(index);
				}, function(index){
					window.parent.layer.close(index);
					$('#add-form').bootstrapValidator('disableSubmitButtons', false);
				});
	        },
	        fields: {
	            title: {
	                validators: {
	                	notEmpty: {
	                    },
	                    stringLength: {
	                        min: 2,
	                        max: 50,
	                    }
	                }
	            },
	            receiver_no: {
	                validators: {
	                    stringLength: {
	                        min: 2,
	                        max: 20,
	                    },
	    				callback: {
		                    callback:function(value, validator){
		                    	if (value =='') {
		                    		$('#add-form').find("input[name='realname']").val('系统管理员');
		                    		return true;
		                    	}
		                    	
		                    	if (value.length>=2 && value.length<=20) {
		                    		var flag = true;
		    						$.ajax({ 
		    							type : 'post',  
		    					    	url : "{:U('Check/existUserVisible')}",  
		    					     	data : {user_no: value},
		    					     	dataType: 'json', 
		    					    	async : false,  
		    					      	success : function(response){
		    					      		$('#add-form').find("input[name='realname']").val(response.realname);
			    							flag = response.status;
		    					    	}  
		    						}); 
		    						
		    						return flag;
	    						}
	    						return false;
	    					}
	    				}
	                }
	            },
	            content: {
	            	validators: {
	            		notEmpty: {
	                    }
	                }
	            }
	        }
	    });
	</script>
	</body>
</html>